package com.yiqi.liebang.feature.home.view.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.GlideImageLoader;
import com.yiqi.liebang.entity.bo.HomeBo;
import com.yiqi.liebang.entity.enums.CertificateImage;
import com.yiqi.liebang.feature.home.view.AskActivity;
import com.yiqi.liebang.feature.home.view.AskDetailActivity;
import com.yiqi.liebang.feature.home.view.BannerWebActivity;
import com.yiqi.liebang.feature.home.view.ClassifyActivity;
import com.yiqi.liebang.feature.home.view.QuestionListActivity;
import com.yiqi.liebang.feature.home.view.TopicInfoActivity;
import com.yiqi.liebang.feature.login.view.LoginActivity;
import com.yiqi.liebang.feature.mine.view.AllCertificationActivity;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.setting.view.ImageActivity;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends BaseMultiItemQuickAdapter<MultipleItem, BaseViewHolder> {
    Banner mBannerView;
    HomeBo homeBo;
    Activity mContext;

    public HomeAdapter(List<MultipleItem> data, HomeBo homeBo, Activity mContext) {
        super(data);
        this.homeBo = homeBo;
        this.mContext = mContext;
        addItemType(MultipleItem.ITEM_BANNER, R.layout.home_list_banner);
        addItemType(MultipleItem.ITEM_MENU, R.layout.home_list_menu);
        addItemType(MultipleItem.ITEM_QUESTION, R.layout.home_list_question);
        addItemType(MultipleItem.ITEM_TOPIC, R.layout.home_list_topic);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultipleItem item) {
        if (homeBo == null) return;
        switch (helper.getItemViewType()) {
            case MultipleItem.ITEM_BANNER:

                TextView tvContent = helper.getView(R.id.tv_home_banner_title);
                TextView tvAuthor = helper.getView(R.id.tv_home_banner_author);
                mBannerView = helper.getView(R.id.banner);
                if (homeBo == null || homeBo.getIndexBanner() == null) return;
                //设置图片加载器
                mBannerView.setImageLoader(new GlideImageLoader());
                //设置图片集合
                List<String> banners = new ArrayList<>();
                for (int i = 0; i < homeBo.getIndexBanner().size(); i++) {
                    banners.add(homeBo.getIndexBanner().get(i).getBannerUrl());
                }
                mBannerView.setImages(banners);
                //banner设置方法全部调用完毕时最后调用
                mBannerView.start();

                mBannerView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        tvContent.setText(homeBo.getIndexBanner().get(position).getTitle());
                        tvAuthor.setText(homeBo.getIndexBanner().get(position).getContent());
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                mBannerView.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        if (homeBo.getIndexBanner().get(position).getTurnType().equals("0")) {
                            if (!TextUtils.isEmpty(homeBo.getIndexBanner().get(position).getTurnUrl())) {
                                Intent intent = new Intent(mContext, BannerWebActivity.class);
                                intent.putExtra("banner", homeBo.getIndexBanner().get(position));
                                mContext.startActivity(intent);
                            }

                        } else if (homeBo.getIndexBanner().get(position).getTurnType().equals("1")) {
                            if (!TextUtils.isEmpty(homeBo.getIndexBanner().get(position).getUserUid())) {
                                Intent intent = new Intent(mContext, UserInfoActivity.class);
                                intent.putExtra("is_view_my", false);
                                intent.putExtra("visitorUid", homeBo.getIndexBanner().get(position).getUserUid());
                                mContext.startActivity(intent);
                            }
                        }
                    }
                });

                break;
            case MultipleItem.ITEM_MENU:
                RecyclerView rvMenu = helper.getView(R.id.rv_menu);
                rvMenu.setLayoutManager(new GridLayoutManager(AM.app(), 4));
                MenuAdapter menuAdapter = new MenuAdapter();
                menuAdapter.bindToRecyclerView(rvMenu);
                List<HomeBo.AppClassifyBean> appClassifyBeans = homeBo.getAppClassify();
                menuAdapter.setNewData(appClassifyBeans);
                menuAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (!AM.user().isLogin()) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }
                        if (position == appClassifyBeans.size() - 1) {
                            Intent intent = new Intent(mContext, ClassifyActivity.class);
                            intent.putExtra("isSelect", false);
                            mContext.startActivity(intent);
                        } else {

                            Intent intents = new Intent(mContext, QuestionListActivity.class);
                            intents.putExtra("type_id", menuAdapter.getData().get(position).getId());
                            intents.putExtra("type_name", menuAdapter.getData().get(position).getClassify());
                            mContext.startActivity(intents);
                        }

                    }
                });
                break;
            case MultipleItem.ITEM_QUESTION:
                RecyclerView rvQuestion = helper.getView(R.id.rv_home_question);
                rvQuestion.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
                rvQuestion.setLayoutManager(new LinearLayoutManager(AM.app()));
                QuestionAdapter questionAdapter = new QuestionAdapter();
                questionAdapter.bindToRecyclerView(rvQuestion);
                questionAdapter.setNewData(homeBo.getQuestion());

                questionAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (!AM.user().isLogin()) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }

                        Intent intent = new Intent(mContext, AskDetailActivity.class);
                        intent.putExtra("answer_id", questionAdapter.getData().get(position).getId());
                        mContext.startActivity(intent);


                    }
                });

                questionAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        if (!AM.user().isLogin()) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }

                        switch (view.getId()) {
                            case R.id.iv_user_question_head:
                                Intent intent = new Intent(mContext, UserInfoActivity.class);
                                intent.putExtra("is_view_my", false);
                                intent.putExtra("visitorUid", questionAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                                mContext.startActivity(intent);
                                break;
                            case R.id.iv_user_position:
                                Intent intentWork = new Intent(mContext, ImageActivity.class);
                                intentWork.putExtra("type", CertificateImage.ZHIYE.getIndex());
                                intentWork.putExtra("userid",questionAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                                mContext.startActivity(intentWork);
                                break;
                            case R.id.iv_user_certification:
                                Intent intentInfo = new Intent(mContext, ImageActivity.class);
                                intentInfo.putExtra("type", CertificateImage.BASIC.getIndex());
                                intentInfo.putExtra("userid",questionAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                                mContext.startActivity(intentInfo);
                                break;
                        }
                    }
                });

                helper.getView(R.id.btn_home_ask_more).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!AM.user().isLogin()) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }
                        Intent intents = new Intent(mContext, ClassifyActivity.class);
                        mContext.startActivity(intents);
                    }
                });

                helper.getView(R.id.btn_home_ask).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mContext.startActivity(new Intent(AM.app(), AskActivity.class));
                    }
                });
                helper.getView(R.id.btn_home_yuejian).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!AM.user().isLogin()) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }
                        mContext.startActivity(new Intent(AM.app(), ClassifyActivity.class));
                    }
                });
                break;
            case MultipleItem.ITEM_TOPIC:
                RecyclerView rvTopic = helper.getView(R.id.rv_home_topic);
                rvTopic.setLayoutManager(new LinearLayoutManager(AM.app()));
                rvTopic.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
                TopicAdapter topicAdapter = new TopicAdapter();
                topicAdapter.bindToRecyclerView(rvTopic);
                topicAdapter.setNewData(homeBo.getTopic());
                topicAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (!AM.user().isLogin()) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }
                        Intent intent = new Intent(mContext, TopicInfoActivity.class);
                        intent.putExtra("topic", topicAdapter.getData().get(position).getId());
                        intent.putExtra("user_id", topicAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                        mContext.startActivity(intent);
                    }
                });

                topicAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        if (!AM.user().isLogin()) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }
                        switch (view.getId()) {
                            case R.id.iv_user_topic:
                                Intent intent = new Intent(mContext, UserInfoActivity.class);
                                intent.putExtra("is_view_my", false);
                                intent.putExtra("visitorUid", topicAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                                mContext.startActivity(intent);
                                break;
                            case R.id.iv_user_position:
                                Intent intentWork = new Intent(mContext, ImageActivity.class);
                                intentWork.putExtra("type", CertificateImage.ZHIYE.getIndex());
                                intentWork.putExtra("userid",topicAdapter.getData().get(position).getUserAccountFormMap().getUserUid() );
                                mContext.startActivity(intentWork);
                                break;
                            case R.id.iv_user_certification:
                                Intent intentInfo = new Intent(mContext, ImageActivity.class);
                                intentInfo.putExtra("type", CertificateImage.BASIC.getIndex());
                                intentInfo.putExtra("userid",topicAdapter.getData().get(position).getUserAccountFormMap().getUserUid() );
                                mContext.startActivity(intentInfo);
                                break;
                        }

                        }

                });
                Banner banner = helper.getView(R.id.banner);
                //设置图片加载器
                banner.setImageLoader(new GlideImageLoader());
                //设置图片集合
                List<String> bannerList = new ArrayList<>();
                for (int i = 0; i < homeBo.getTopicBanner().size(); i++) {
                    bannerList.add(homeBo.getTopicBanner().get(i).getBannerUrl());
                }
                //设置图片集合
                banner.setImages(bannerList);
                //banner设置方法全部调用完毕时最后调用
                banner.start();
                banner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        if (homeBo.getTopicBanner().get(position).getTurnType().equals("0")) {
                            if (!TextUtils.isEmpty(homeBo.getIndexBanner().get(position).getTurnUrl())) {
                                Intent intent = new Intent(mContext, BannerWebActivity.class);
                                intent.putExtra("banner", homeBo.getTopicBanner().get(position));
                                mContext.startActivity(intent);
                            }
                        } else if (homeBo.getTopicBanner().get(position).getTurnType().equals("1")) {
                            if (!TextUtils.isEmpty(homeBo.getTopicBanner().get(position).getUserUid())) {
                                Intent intent = new Intent(mContext, UserInfoActivity.class);
                                intent.putExtra("is_view_my", false);
                                intent.putExtra("visitorUid", homeBo.getTopicBanner().get(position).getUserUid());
                                mContext.startActivity(intent);
                            }
                        }
                    }
                });


                TextView tvAllClassify = helper.getView(R.id.tv_home_all_classify);
                tvAllClassify.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!AM.user().isLogin()) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                            return;
                        }
                        Intent intent = new Intent(mContext, ClassifyActivity.class);
                        intent.putExtra("isSelect", false);
                        mContext.startActivity(intent);
                    }
                });
                break;
        }


    }
}
